package com.tss.dao.impl;

import com.tss.dao.TransactionRecordDAO;
import com.tss.model.TransactionRecord;
import com.tss.utils.DBUtil;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 交易记录表操作实现类
 */
public class TransactionRecordDAOImpl implements TransactionRecordDAO {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public boolean insert(TransactionRecord record) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean result = false;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO transaction_record (dest_code, ticket_type, amount, payment_method, time, is_cancelled) " +
                    "VALUES (?, ?, ?, ?, ?, ?)";
            ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, record.getDestCode());
            ps.setString(2, record.getTicketType());
            ps.setDouble(3, record.getAmount());
            ps.setString(4, record.getPaymentMethod());
            ps.setString(5, record.getTime().format(formatter));
            ps.setInt(6, record.isCancelled() ? 1 : 0);
            
            int affectedRows = ps.executeUpdate();
            if (affectedRows > 0) {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                if (generatedKeys.next()) {
                    record.setId(generatedKeys.getInt(1));
                }
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, null);
        }
        
        return result;
    }

    @Override
    public List<TransactionRecord> selectCancelled() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<TransactionRecord> records = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM transaction_record WHERE is_cancelled = 1";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            
            while (rs.next()) {
                TransactionRecord record = new TransactionRecord();
                record.setId(rs.getInt("transaction_id"));
                record.setDestCode(rs.getString("dest_code"));
                record.setTicketType(rs.getString("ticket_type"));
                record.setAmount(rs.getDouble("amount"));
                record.setPaymentMethod(rs.getString("payment_method"));
                record.setTime(LocalDateTime.parse(rs.getString("time"), formatter));
                record.setCancelled(rs.getInt("is_cancelled") == 1);
                records.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, rs);
        }
        
        return records;
    }
    
    @Override
    public boolean cancelTransaction(int transactionId) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean result = false;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE transaction_record SET is_cancelled = 1 WHERE transaction_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, transactionId);
            
            int affectedRows = ps.executeUpdate();
            result = affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, null);
        }
        
        return result;
    }
} 